<template>
  <div style="width: 100%">
    <div class="btn-big">
      <el-button plain size="small" @click="addUser"><i class="el-icon-plus"></i>选择成员</el-button>
      <div class="number">已选择<span>{{ multipleSelection.length }}</span>人</div>
    </div>
    <ul class="images-list" v-if="multipleSelection && multipleSelection.length>0">
      <li v-for="(item ,index) in multipleSelection" :key="index"><img src="@/assets/images/profile.jpg"/> {{ item.name }}
        <div class="close" @click="handerDel(item,'out',index)">
          <i class="el-icon-close"></i>
        </div>
      </li>
    </ul>
    <el-dialog custom-class="check-group-dialog" append-to-body :close-on-click-modal="false" :close-on-press-escape="false" :visible.sync="openDialog" width="1000px" :before-close="onHandleClose">
      <div slot="title" style="font-size: 14px;font-weight: 500;">
        选择成员
      </div>
      <div v-if="openDialog" class="check-group-main">
        <div class="check-group-item">
          <div class="item-head">
            <el-input class="search-input" size="small" placeholder="输入成员名称" suffix-icon="el-icon-search"/>
          </div>
          <ul class="gourp-list">
            <li class="active" ><span>肖洁</span></li>
            <li><span>闵雨薇</span></li>
          </ul>
        </div>
        <div class="check-group-item">
          <div class="item-head">
            <span style="font-size: 16px;font-weight: 400;color: rgba(0, 0, 0, .45);margin-left: 4px;">（{{ multipleSelection.length }}）</span>
          </div>
          <el-table :data="tableData"
                    ref="multipleTable"
                    tooltip-effect="dark"
                    style="width: 100%"
                    @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" width="70"></el-table-column>
            <el-table-column prop="name" label="成员姓名" width="180"></el-table-column>
            <el-table-column prop="department" label="部门" width="180"></el-table-column>
            <el-table-column prop="phonenumber" label="手机"></el-table-column>
            <template slot="empty">
              <TableEmpty/>
            </template>
          </el-table>
        </div>
        <div class="check-group-item">
          <div class="item-head">
            <span class="title">已选（{{ multipleSelection.length }}）</span>
            <span class="btn-text" @click="delAll">清空</span>
          </div>
          <ul class="user-list">
            <li v-for="(item ,index) in multipleSelection" :key="index">
              <span>{{ item.name }}</span>
              <i class="el-icon-delete" @click="handerDel(item,'in')"></i>
            </li>
          </ul>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
      <el-button @click="onHandleClose">取消</el-button>
      <el-button type="primary" @click="onSubmit">确定</el-button>
    </span>
    </el-dialog>
  </div>

</template>
<script>
export default {
  props: {
    hasValue: {
      type: Array,
      default: () => {
        return [];
      },
    }
  },
  data() {
    return {
      openDialog: false,
      multipleSelection: [],
      showTime:null,
      tableData: [
        {id: '1', name: '小红', phonenumber: '18207428337', department: '人事部',},
        {id: '2', name: '小军', phonenumber: '12207426737', department: '技术部',},
        {id: '3', name: '小力', phonenumber: '18607348337', department: '销售部',},
        {id: '4', name: '小海', phonenumber: '18307427337', department: '技术部',},
      ]
    }
  },
  created() {
    this.multipleSelection = this.hasValue;
  },
  watch:{
  },
  methods: {
    delAll(){
      this.multipleSelection.forEach((item=>{
        this.$refs.multipleTable.toggleRowSelection(item,false);
      }))
    },
    handerDel(item,type,index) {
      if(type=='in'){
        this.$refs.multipleTable.toggleRowSelection(item,false);
      }else {
        this.multipleSelection.splice(index,1);
        this.$emit('getValue', this.multipleSelection);
      }

    },
    toggleSelection(rows) {
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row,true);
        });
      } else {
        this.$refs.multipleTable.clearSelection();
      }
    },
    handleSelectionChange(val) {
      this.multipleSelection=[];
      this.multipleSelection = val;
    },
    addUser() {
      this.$emit('getValue', this.multipleSelection);
      this.openDialog = true;
      this.$nextTick(()=>{
        if (this.multipleSelection && this.multipleSelection.length > 0) {
          this.toggleSelection(this.multipleSelection)
        }
      })




    },
    onHandleClose() {
      this.openDialog = false;
    },
    onSubmit() {
      this.$emit('getValue', this.multipleSelection,new Date().getTime());
      this.openDialog = false;
    }
  }
}
</script>
<style lang="scss" scoped>
.check-group-main {
  display: grid;
  grid-template-columns: 200px 1fr 200px;
  min-height: 540px;

  .check-group-item {
    &:not(:last-child) {
      border-right: 1px solid #e9e9e9;
    }

    .item-head {
      background: #fafafa;
      border-bottom: 1px solid #e9e9e9;
      height: 48px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 12px;

      .title {
        font-size: 14px;
        font-weight: 600;
        line-height: 22px;
      }

      .btn-text {
        color: #3070ff;
        text-decoration: none;
        background-color: transparent;
        outline: none;
        cursor: pointer;
        transition: color .3s;
      }
    }

    .search-list {
      padding: 8px 8px;

      .filter-item {
        margin-bottom: 8px;

        .head {
          font-size: 14px;
          color: rgba(0, 0, 0, .85);
          margin-bottom: 8px;
        }
      }
    }
  }
}
</style>
<style>
.check-group-dialog .el-dialog__body {
  padding: 0 !important;
}

.threshold-content {
  padding: 12px;
}

.input-rows {
  display: flex;
  align-items: center;
}

.input-rows .delimiter {
  margin: 0 8px;
  font-size: 14px;
  color: rgba(0, 0, 0, .25);
}

.threshold-content .submit-button {
  width: 100%;
  margin-top: 12px;
}

.tags-dropdown {
  width: 450px;
  font-size: 14px;
  font-weight: 400;
}

.tags-dropdown .dropdown-head {
  padding: 12px 16px;
  border-bottom: 1px solid #e9e9e9;
}

.dropdown-body {
  padding: 12px;
}

.dropdown-body .tag-record {
  margin-top: 16px;
}

.tag-record .tag-head {
  font-size: 12px;
  color: #666;
}

.tag-record .tags-item {
  margin-top: 8px;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr 1fr;
  column-gap: 8px;
  row-gap: 8px;
  text-align: center;
}

.tags-item .tag {
  margin-bottom: 8px;
  margin-right: 8px;
  font-size: 14px;
  padding: 5px 12px;
  background: rgba(0, 0, 0, .06);
  border-radius: 2px;
}

.tags-item .active {
  background-color: #2f54eb;
  color: #fff;
}

.dropdown-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #f5f5f5;
  padding: 10px 16px;
  border-top: 1px solid #e9e9e9;
}

.dropdown-footer .unkown-button {
  color: #3070ff;
  border: 1px solid #3070ff;
  border-radius: 4px;
  text-shadow: none;
  font-size: 14px;
  line-height: 22px;
  width: 88px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.images-list {
  list-style: none;
}

.btn-big {
  display: flex;
  align-items: center;

  .el-button {
    margin-right: 10px;
  }

  .number {
    span {
      color: #C03639;
      margin: 0px 5px;
    }
  }
}

.images-list {
  display: flex;
  display: flex;
  padding: 8px 8px 0;
  margin: 20px 0px 10px 0px;
  background: #f3f3f3;
  border-radius: 2px;

  li {
    position: relative;
    width: 126px;
    height: 48px;
    display: flex;
    align-items: center;
    padding: 8px;
    margin: 0 8px 8px 0;
    background: #fff;
    border: 1px solid #e9e9e9;

    .close {
      position: absolute;
      right: 0px;
      top: 0px;
      width: 15px;
      height: 15px;
      background: #2f333c;
      cursor: pointer;
      display: flex;
      align-items: center;

      i {
        color: #fff;
      }
    }

    img {
      width: 25px;
      height: 25px;
      margin-right: 10px;
    }
  }
}

.user-list {
  padding: 0px 10px;

  li {
    display: flex;
    justify-content: space-between;
    height: 40px;
    align-items: center;

    i {
      cursor: pointer;
    }
  }

}

.gourp-list {
  padding: 0px 10px;
  margin-top: 10px;

  li {
    display: flex;
    justify-content: space-between;
    height: 40px;
    align-items: center;
    padding: 0px 15px;
    cursor: pointer;

    &.active {
      background: #ebf2ff;
    }
  }
}
</style>
